package com.example.handpose_front.MainPage;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.handpose_front.BaseActivity;
import com.example.handpose_front.ExtendedPage.LearningTrackActivity;
import com.example.handpose_front.LearnAndReviewPage.LearnActivity;
import com.example.handpose_front.LearnAndReviewPage.ReviewActivity;
import com.example.handpose_front.R;
import com.example.handpose_front.VideoPage.VideoActivity;
import com.example.handpose_front.dto.DataResponse;
import com.example.handpose_front.utils.LocalStorage;
import com.example.handpose_front.utils.OKHttpUtil.HttpUrl;
import com.example.handpose_front.utils.OKHttpUtil.OKHttpUtil;
import com.example.handpose_front.utils.OKHttpUtil.ResponseBody;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends BaseActivity implements View.OnClickListener{
    View learnTV;
    View reviewTV;
    View learningTrackView;
    View signView;
    View recommendInfoView;

    // 响应体
    private DataResponse dataResponse;
    Map<String, String> map = new HashMap<>();

    String userPhone = LocalStorage.getCurrentPhone();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == learnTV.getId()) {
            Intent intent = new Intent();
            intent.setClass(this, LearnActivity.class);
            startActivity(intent);
        }
        if (v.getId() == reviewTV.getId()) {
            Intent intent = new Intent();
            intent.setClass(this, ReviewActivity.class);
            startActivity(intent);
        }
        if (v.getId() == learningTrackView.getId()) {
            Intent intent = new Intent();
            intent.setClass(this, LearningTrackActivity.class);
            startActivity(intent);
        }
        if (v.getId() == recommendInfoView.getId()) {
            Intent intent = new Intent();
            intent.setClass(this, VideoActivity.class);
            startActivity(intent);
        }
        if (v.getId() == signView.getId()) {
            map.clear();

            map.put("userPhone", userPhone);
            String response = OKHttpUtil.postSyncRequest(HttpUrl.getBaseUrl(), map, "userStatus", "usersign");
            dataResponse = ResponseBody.getResponseBody(response);
            if (dataResponse.getCode().equals("200")) {
                String dayOfMonth = dataResponse.getData().get("dayOfMonth").toString();
                Log.d("dayOfMonth:", dayOfMonth);
                // 签到成功
                Toast.makeText(this, "签到成功", Toast.LENGTH_SHORT).show();
            } else {
                // 提示错误信息
                Toast.makeText(this, "操作失败", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void initEvent() {
        learnTV.setOnClickListener(this);
        reviewTV.setOnClickListener(this);
        learningTrackView.setOnClickListener(this);
        signView.setOnClickListener(this);
        recommendInfoView.setOnClickListener(this);
    }

    private void initView() {
        learnTV = findViewById(R.id.learn_tv);
        reviewTV = findViewById(R.id.review_tv);
        learningTrackView = findViewById(R.id.learning_track_view);
        signView = findViewById(R.id.usersign_view);
        recommendInfoView = findViewById(R.id.recommend_info_view);
    }

}